
    /**  
    * @Title: Video.java
    * @Package com.baidu.aip.speech
    * @Description: 
    * @author likai
    * @date 2018年1月3日
    * @version V1.0  
    */
    
package com.utils.voice;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
    * @ClassName: Video
    * @Description: 视频、语音转码
    * @author likai
    * @date 2018年1月3日
    *
    */

public class ConvertVideoUtil{

	public static Logger log = LoggerFactory.getLogger(ConvertVideoUtil.class);
	/* 
	    * @Description:mp3转pcm格式
	    * 
	    * @param:
	    * 
	    * webroot :ffmpeg.exe 文件路径（例如 f:\\ffmpeg\\bin）
		* sourcePath:资源文件路径
		* targetPath:生成文件路径
	    *
	    */
    public static void mp3ToPcm(String webroot,String sourcePath, String targetPath){
        Runtime run = null;
        try {
            run = Runtime.getRuntime();
            StringBuffer cmd=new StringBuffer();
            cmd.append(webroot).append("/ffmpeg -y -i ").append(sourcePath).append(" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 ").append(targetPath);
            //Process p=run.exec("/usr/local/ffmpeg/bin/ffmpeg -y -i /home/server/pic/c53bd34d637b431eb4c1752e22051587.mp3 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 /home/server/pic/c53bd34d637b431eb4c1752e22051587.pcm");
            String[] cmds=new String[]{"sh","-c",cmd.toString()};
            Process p=run.exec(cmds);
            //释放进程
            log.info(loadStream(p.getInputStream()));
            p.getOutputStream().close();
            p.getInputStream().close();
            p.getErrorStream().close();
            int code=p.waitFor();
            if (code!=0) {
            	log.error("文件转换失败");
			}
        } catch (Exception e) {
        	log.error("文件转换出错："+e.getMessage());
        }finally{
            //run调用lame解码器最后释放内存
            run.freeMemory();
        }
    }
    
    /* 
	    * @Description:arm转MP3
	    * 
	    * @param:
	    * 
	    * webroot :ffmpeg.exe 文件路径（例如 f:\\ffmpeg\\bin）
		* sourcePath:资源文件路径
		* targetPath:生成文件路径
	    *
	    */
	 public static void armToMp3(String webroot,String sourcePath, String targetPath){
	     Runtime run = null;
	     try {
	         run = Runtime.getRuntime();
	         StringBuffer cmd=new StringBuffer();
	         cmd.append(webroot).append("/ffmpeg  -i ").append(sourcePath).append("   ").append(targetPath);
	         Process p=run.exec(cmd.toString());
	         log.info(loadStream(p.getInputStream()));//打印命令执行结果
	         //释放进程
	         p.getOutputStream().close();
	         p.getInputStream().close();
	         p.getErrorStream().close();
	         int code=p.waitFor();
	         if (code!=0) {
	         	log.error("文件转换失败");
			 }
	     } catch (Exception e) {
	     	log.error("文件转换出错："+e.getMessage());
	     }finally{
	         //run调用lame解码器最后释放内存
	         run.freeMemory();
	     }
	 }
	 
	 /* 
	    * @Description:arm转MP3
	    * 
	    * @param:
	    * 
	    * webroot :ffmpeg.exe 文件路径（例如 f:\\ffmpeg\\bin）
		* sourcePath:资源文件路径
		* targetPath:生成文件路径
	    *
	    */
	 public static void armToWav(String webroot,String sourcePath, String targetPath){
	     Runtime run = null;
	     try {
	         run = Runtime.getRuntime();
	         StringBuffer cmd=new StringBuffer();
	         cmd.append(webroot).append("/ffmpeg -acodec libamr_nb -i ").append(sourcePath).append("   ").append(targetPath);
	         log.info(cmd.toString());
	         Process p=run.exec(cmd.toString());
	         //释放进程
	         log.info(loadStream(p.getInputStream()));
	         p.getOutputStream().close();
	         p.getInputStream().close();
	         p.getErrorStream().close();
	         int code=p.waitFor();
	         if (code!=0) {
	         	log.error("文件转换失败");
			 }
	     } catch (Exception e) {
	     	log.error("文件转换出错："+e.getMessage());
	     }finally{
	         //run调用lame解码器最后释放内存
	         run.freeMemory();
	     }
	 }
	 
	 /* 
	    * @Description:MP3转arm
	    * 
	    * @param:
	    * 
	    * webroot :ffmpeg.exe 文件路径（例如 f:\\ffmpeg\\bin）
		* sourcePath:资源文件路径
		* targetPath:生成文件路径
	    *
	    */
	 public static void mp3ToArm(String webroot,String sourcePath, String targetPath){
	     Runtime run = null;
	     try {
	         run = Runtime.getRuntime();
	         StringBuffer cmd=new StringBuffer();
	         cmd.append(webroot).append("/ffmpeg  -i ").append(sourcePath).append(" -ac 1 -ar 8000 ").append(targetPath);
	         String[] cmds=new String[]{"sh","-c",cmd.toString()};
	         Process p=run.exec(cmds);
	         //释放进程
	         log.info(loadStream(p.getInputStream()));
	         p.getOutputStream().close();
	         p.getInputStream().close();
	         p.getErrorStream().close();
	         int code=p.waitFor();
	         if (code!=0) {
	         	log.error("文件转换失败");
				}
	     } catch (Exception e) {
	     	log.error("文件转换出错："+e.getMessage());
	     }finally{
	         //run调用lame解码器最后释放内存
	         run.freeMemory();
	     }
	 }
 // read an input-stream into a String  
    static String loadStream(InputStream in) throws IOException  
    {  
        int ptr = 0;  
        in = new BufferedInputStream(in);  
        StringBuffer buffer = new StringBuffer();  
        while ((ptr = in.read()) != -1)  
        {  
            buffer.append((char) ptr);  
        }  
        return buffer.toString();  
  
    }  
    public static void main(String[] args) {
    	Runtime run = null;
        try {
            run = Runtime.getRuntime();
            // /usr/local/ffmpeg/bin/ffmpeg -y -i /home/server/pic/bfcd386555214c339fc3b8cfca9e5ad5.mp3 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 /home/server/pic/bfcd386555214c339fc3b8cfca9e5ad5.pcm
          //  Process p=run.exec(webroot+"/ffmpeg -y  -i "+sourcePath+" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 "+targetPath);
            Process p=run.exec("F:/ffmpeg/bin/ffmpeg -y -i E:/pic/c53bd34d637b431eb4c1752e22051587.mp3 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 E:/pic/c53bd34d637b431eb4c1752e22051587.pcm");
            log.info("文件转换完毕");
            //释放进程
            p.getOutputStream().close();
            p.getInputStream().close();
            p.getErrorStream().close();
           int code= p.waitFor();
           log.info(code+"");
        } catch (Exception e) {
        	log.error("文件转换出错："+e.getMessage());
        }finally{
            //run调用lame解码器最后释放内存
            run.freeMemory();
        }
        log.info("---------------1");
	}
}
